/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GameObject;

import java.awt.Graphics2D;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import java.util.Timer;
import javax.imageio.ImageIO;
/**
 *
 * @author NGUYEN VAN TA
 */
public class Background  extends VisibleGameEntity{
    @Override
    public boolean Init(int _numberSprite[], int _Width,int _Heigth,String StrResource[],int isprite, int _nDelay)
    {
            _nsprite = _numberSprite.length;
            _sprite = new Sprite[_nsprite];
            width = _Width;
            height = _Heigth;
            nDelay = _nDelay;
            
            for (int i = 0; i < _nsprite; i++)
            {
                //Reource anh/Heart/000.png""
                try
                {
                    Image[] image = new Image[_numberSprite[i]];

                    for (int j = 0; j < _numberSprite[i]; j++)
                    {
                        image[j] = ImageIO.read(new File(StrResource[i] + j + ".png"));
                    }
                    _sprite[i] = new Sprite(100, 20, _Width,_Heigth ,image);
                    _isprite = isprite;
                }
                catch(IOException ex)
                {
                    return false;
                }
            }
            return true;
    }
    
    @Override
    public VisibleGameObject Clone()
    {
        VisibleGameObject obj = new Background();
        obj._nsprite = this._nsprite;
        obj.x = this.x;
        obj.y = this.y;
        obj._isprite = this._isprite;
        obj.width = this.width;
        obj.height = this.height;
        obj.nDelay = 0;
        obj._sprite = new Sprite[_nsprite];
        try
        {
        for(int i = 0;i < this._sprite.length;i++)
        {
            obj._sprite[i] = new Sprite(this.x, this.y, this._sprite[i]._width, this._sprite[i]._height, this._sprite[i]._images);
        }
        }
        catch(Exception io)
        {
            
        }
        return obj;
    }
    
    @Override
    public void Update(Timer gameTime)
    {
        this.Update(gameTime);
    }
    
    @Override
    public void Update(Timer gameTime,int x, int y, int w, int h)
    {       
        if(nDelay == 0 )
        {
            this.width = w;
            this.height = h;
            this.x = x;
            this.y = y;
            
            for (int i = 0; i < _sprite.length; i++)
            {
                _sprite[i].update(gameTime,x,y,w,h,_sprite[i]._selectedIndex);
            }
        }
        else 
        {
            nDelay--;
        }
    }
    
    @Override
    public void Draw(Timer gameTime,Graphics2D graphic)
    {
        // _isprite dduocj chọn ispite
        //for (int i = 0; i <  _sprite.length; i++)
        //{
        if(_isprite >= 0 && _isprite < _sprite.length)
            _sprite[_isprite].Draw(gameTime,graphic);
        //}
    }
}
